package com.feige.result;



import com.feige.constant.Constants;
import com.feige.utils.string.StringUtils;

import java.util.HashMap;

public class AjaxResult extends HashMap<String,Object> {

    public AjaxResult(int code,String msg,Object data){
        super.put(Constants.CODE_TAG,code);
        super.put(Constants.MSG_TAG,msg);
        super.put(Constants.DATA_TAG,data);
    }
    public AjaxResult(int code,String msg){
        super.put(Constants.CODE_TAG,code);
        super.put(Constants.MSG_TAG,msg);
    }

    public AjaxResult(ResultEnumCode resultEnumCode){
        super.put(Constants.CODE_TAG,resultEnumCode.getCode());
        super.put(Constants.MSG_TAG,resultEnumCode.getMsg());
    }
    public AjaxResult(){
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static AjaxResult success(){
        return new AjaxResult(ResultEnumCode.success);
    }

    public static AjaxResult success(int code, String msg){
        return new AjaxResult(code,msg);
    }

    public static AjaxResult success(int code, String msg,Object data){
        return new AjaxResult(code,msg,data);
    }
    public static AjaxResult success(Object data){
        AjaxResult ajaxResult = new AjaxResult(ResultEnumCode.success);
        if (StringUtils.isNotNull(data)){
            ajaxResult.put(Constants.DATA_TAG,data);
        }
        return ajaxResult;
    }

    /**
     * 失败
     * @param data
     * @return
     */
    public static AjaxResult error(Object data){
        AjaxResult ajaxResult = new AjaxResult(ResultEnumCode.error);
        if (StringUtils.isNotNull(data)){
            ajaxResult.put(Constants.DATA_TAG,data);
        }
        return ajaxResult;
    }
    public static AjaxResult error(int code, String msg,Object data){
        return new AjaxResult(code,msg,data);
    }
    public static AjaxResult error(int code, String msg){
        return new AjaxResult(code,msg);
    }
    public static AjaxResult error(){
        return new AjaxResult(ResultEnumCode.error);
    }
}
